<?php
if($_SESSION['proses']!=1){
	echo "Akses tidak diizinkan";
	exit();
}

$act = filter($_GET['act']);
$field = filter($_GET['field']);
$q = filter($_GET['q']);
?>
<?php
if(empty($act)){	
?>		
					<!-- box / title -->
					<div class="title">
						<h5>Data Produksi</h5>								
						<div class="search global-nav">
							<form method="GET" class="form-search js-search-form" action="index.php?pages=limbah_prod" id="global-nav-search" style="width:350px">
								<input type="hidden" name="pages" value="limbah_prod">
								<select name="field" id="birthday_month" aria-label="Month" class="" >
									<option value="" <?php echo ($field=="")?"selected":"";?>>Cari Berdasarkan:</option>
									<option value="kode_prod" <?php echo ($field=="kode_prod")?"selected":"";?>>Kode Produksi</option>
									<option value="id_formula" <?php echo ($field=="id_formula")?"selected":"";?>>Kode Formula</option>
									<option value="tgl_persiapan" <?php echo ($field=="tgl_persiapan")?"selected":"";?>>Tgl. Persiapan</option>
									<option value="tgl_prod" <?php echo ($field=="tgl_prod")?"selected":"";?>>Tgl. Produksi</option>
									<option value="tgl_selesai" <?php echo ($field=="tgl_selesai")?"selected":"";?>>Tgl. Selesai</option>																		
								</select>

				            	<label class="visuallyhidden" for="search-query">Search query</label>
				            	<input class="search-input" type="text" id="search-query" placeholder="Search" name="q" value="<?php echo $q;?>">
				            	<span class="search-icon js-search-action">
				              		<button type="submit" class="icon nav-search" tabindex="-1">
				              			<span class="visuallyhidden">Search</span>
				              		</button>
				            	</span>
				            	<input disabled="disabled" class="search-input search-hinting-input" type="text" id="search-query-hint" autocomplete="off" spellcheck="false">				              					              
							</form>
						</div>
					</div>
					<!-- end box / title -->
					<div class="table">										
						<table>
							<thead>
								<tr>
									<th class="selected"><input type="checkbox" class="checkall" /></th>
									<th class="action">No.</th>
									<th>Kode Produksi</th>
									<th>Kode Formula</th>
									<th>Jumlah Produksi</th>
									<th>Sisa Produksi</th>
									<th style="background-color: #ffd76e">Limbah Produksi</th>
									<th>Tgl. Persiapan</th>
									<th>Status Persiapan</th>
									<th>Tgl. Produksi</th>
									<th>Status Produksi</th>
									<th>Tgl. Selesai</th>
									<th>Status Selesai</th>									
									<th class="action">#</th>									
								</tr>
							</thead>
							<tbody>
							<?php			

							$q2=$q;

							if($field=='id_formula'){
								$sqlFormula = mysql_query("SELECT id_formula FROM tb_formula WHERE kode LIKE '%$q%'");
								$onoFormula = mysql_num_rows($sqlFormula);
								//echo "SELECT id_formula FROM tb_formula WHERE kode LIKE '%$q%'";
								while ($dataFormula = mysql_fetch_array($sqlFormula)) {
									$qx .= $dataFormula['id_formula'].',';
								}

								if($onoFormula)
									$q2 = substr($qx, 0, -1);
								else
									$q2 = '999999999999999999999';

							}
							
							$addQuery='';


							if(!empty($field)){
								if($field=='id_formula')
									$addQuery .= " WHERE $field IN ($q2) ";
								else
									$addQuery .= " WHERE $field LIKE '%$q2%' ";
							}

							$batas=10;
							$pg=(int) $_GET['pg'];
							if(empty($pg))
							{
								$posisi=0;
								$pg=1;
							}
							else
							{
								$posisi = ($pg-1) * $batas;
							}
							$sqlProd = mysql_query("SELECT * FROM tb_produksi $addQuery ORDER BY id_produksi DESC limit $posisi, $batas");
							//echo "SELECT * FROM tb_produksi $addQuery ORDER BY id_produksi DESC limit $posisi, $batas";
							$no=$posisi+1;
							while($dataProd = mysql_fetch_array($sqlProd)){			
								$dataFormula = mysql_fetch_array(mysql_query("SELECT kode FROM tb_formula WHERE id_formula='$dataProd[id_formula]' LIMIT 1"));		
								//echo "SELECT kode FROM tb_formula WHERE id='$dataProd[id_formula]' LIMIT 1";

								$statusPersiapan = ($dataProd['status_persiapan']==1)? "<div class=icon-ok>&nbsp;</div>":"<div class=icon-remove>&nbsp;</div>";
								$statusProduksi = ($dataProd['status_prod']==1)? "<div class=icon-ok>&nbsp;</div>":"<div class=icon-remove>&nbsp;</div>";
								$statusSelesai = ($dataProd['status_selesai']==1)? "<div class=icon-ok>&nbsp;</div>":"<div class=icon-remove>&nbsp;</div>";								
							?>
								<tr>
									<td class="selected"><input type="checkbox" /></td>
									<td class="center"><?php echo $no;?></td>
									<td class="center"><?php echo $dataProd['kode_prod'];?></td>
									<td class="center"><?php echo $dataFormula['kode'];?></td>
									<td class="center"><?php echo $dataProd['jumlah'];?> Karton</td>
									<td class="center"><?php echo $dataProd['sisa_prod'];?> Cup</td>
									<td class="center" style="background-color: #ffd76e"><?php echo $dataProd['limbah_prod'];?> Kg</td>
									<td class="center"><?php echo $dataProd['tgl_persiapan'];?></td>
									<td class="center"><?php echo $statusPersiapan;?></td>
									<td class="center"><?php echo $dataProd['tgl_prod'];?></td>
									<td class="center"><?php echo $statusProduksi;?></td>
									<td class="center"><?php echo $dataProd['tgl_selesai'];?></td>
									<td class="center"><?php echo $statusSelesai;?></td>
									<td class="action"><a href="?pages=limbah_prod&act=edit&id=<?php echo $dataProd['id_produksi'];?>" class="icon-edit" title="Ubah Data"></a></td>															
								</tr>
							<?php	
								$no++;				
							}

							$sqlProd2 = mysql_query("SELECT * FROM tb_produksi $addQuery ORDER BY id_produksi DESC");
							$jumlahData = mysql_num_rows($sqlProd2);

							$file="?pages=limbah_prod&field=$field&q=$q";
							$jmlhalaman=ceil($jumlahData/$batas);
	
							?>															
							</tbody>
						</table>
						<!-- pagination -->
						<div class="pagination pagination-left">
							<div class="results">
								<span><?php echo "Showing Results ".($posisi+1)."-".($no-1)." of $jumlahData";?></span>
							</div>
							<ul class="pager">
								<?php
								//link ke halaman sebelumnya (previous)
								if($pg > 1){
									$previous=$pg-1;
									echo "<li><a href=$file&pg=1>&laquo; First</a></li>
									<li><a href=$file&pg=$previous>&laquo; Prev</a></li> ";
								}else{ 
									echo "<li class='disabled'>&laquo; First</li>
									<li class='disabled'>&laquo; Prev</li> ";
								}
								?>

								<?php
								//Link untuk Angka
								$angka=($pg > 3 ? " <li class='separator'>...</li> " : " ");
								for($i=$pg-2;$i<$pg;$i++)
								{
								  if ($i < 1) 
								      continue;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= " <li class='current'>$pg</li>";
								for($i=$pg+1;$i<($pg+3);$i++)
								{
								  if ($i > $jmlhalaman) 
								      break;
								  $angka .= "<li><a href='$file&pg=$i'>$i</a></li> ";
								}

								$angka .= ($pg+2<$jmlhalaman ? " <li class='separator'>...</li> <li><a href='$file&pg=$jmlhalaman'>$jmlhalaman</a></li>" : " ");

								echo "$angka";
								?>
								

								<?php
								//link kehalaman berikutnya (Next)
								if($pg < $jmlhalaman)
								{
									$next=$pg+1;
									echo "<li><a href='$file&pg=$next'>Next &raquo;</a></li>
									<li><a href='$file&pg=$jmlhalaman'>Last &raquo;</a></li>";
								}
								else
								{ 
									echo "<li class='disabled'>Next &raquo;</li>
									<li class='disabled'>Last &raquo;</li> ";
								}		
								?>													
							</ul>
						</div>

						<!-- end pagination -->
						<!-- table action -->
						
						<!-- end table action -->						
					</div>

<?php
}elseif ($act=='edit') {

	$id_produksi = (int) filter($_GET['id']);

	$sqlProd = mysql_query("SELECT * FROM tb_produksi WHERE id_produksi=$id_produksi");
	$dataProd = mysql_fetch_array($sqlProd);

	$statusPersiapan = ($dataProd['status_persiapan']==1)? "checked":"";
	$statusProduksi = ($dataProd['status_prod']==1)? "checked":"";
	$statusSelesai = ($dataProd['status_selesai']==1)? "checked":"";	

	$sqlSisa = mysql_query("SELECT id_prod, jumlah_sisa FROM tb_sisa_produksi WHERE id_prod='$id_produksi'");
	//$dataSisa = mysql_fetch_array($sqlRts);

	if(!empty($id_produksi)){
		?>
		<!-- box / title -->
		<div class="title">
			<h5>Ubah Data Limbah Produksi </h5>														
		</div>
		<!-- end box / title -->
		<?php
	}

?>	

	<div class="table">
		<div style="float:left">
		<form method="POST" action="?pages=limbah_prod&act=update">
		<input type="hidden" name="id_produksi" value="<?php echo $dataProd['id_produksi'];?>">
		<table style="width:auto">
		   	<tbody>
		   		<tr>
					<td><label>Kode Formula</label></td>
					<td>:</td>
					<td>
                    <select class="select" style="width: 210px;" name="id_formula" id="id_formula" placeholder="Kode Formula" required disabled>                                           
                      <?php
                            $sqlFormula = mysql_query("SELECT kode,type FROM tb_formula WHERE id_formula='$dataProd[id_formula]'");
                            $dataFormula = mysql_fetch_array($sqlFormula);
                      ?>
                      	<option <?php echo $selected;?> value="<?php echo "$dataFormula[id_formula]";?>"><?php echo "$dataFormula[kode]";?></option>
                    </select>                    
                    </td>
			   	</tr>
			   	<tr>
					<td><label>Jumlah</label></td>
					<td>:</td>
					<td><input type="text" size="30" value="<?php echo "$dataProd[jumlah]";?>" name="jumlah" id="jumlah" placeholder="Jumlah Produksi" readonly> Karton</td>
			   	</tr>		   
			   	<tr>
					<td><label>Jumlah Per Karton</label></td>
					<td>:</td>
					<td><input type="text" size="30" value="<?php echo "$dataProd[jumlah_per_karton]";?>" name="jumlah_per_karton" id="jumlah_per_karton" placeholder="Jumlah Perkarton" readonly> Cup</td>
			   	</tr>
			   	<tr>
					<td><label>Jenis Formula</label></td>
					<td>:</td>
					<td><input type="text" size="30" value="<?php echo $dataFormula['type'];?>" name="type" id="type" placeholder="Jenis Formula" disabled></td>
			   	</tr>
			   	<tr>
					<td><label>Kode Produksi</label></td>
					<td>:</td>
					<td>
						<input type="text" size="30" value="<?php echo "$dataProd[kode_prod]";?>" name="kode_prod" id="kode_prod" placeholder="Kode Produksi" readonly>						
					</td>
			   	</tr>			   
		   		<tr>
					<td><label>Sisa Produksi</label></td>
					<td>:</td>
					<td>
						<input type="text" value="<?php echo $dataProd['sisa_prod'];?>" size="30" name="sisa_prod" id="sisa_prod" placeholder="Sisa Produksi" readonly> Cup						
					</td>
			   	</tr>	
			   	<tr>
					<td><label>Limbah Produksi</label></td>
					<td>:</td>
					<td>
						<input type="text" value="<?php echo $dataProd['limbah_prod'];?>" size="30" name="limbah_prod" id="limbah_prod" placeholder="Limbah Produksi"> Kg						
					</td>
			   	</tr>		   				   			   				   				   
			 	<tr>
			 		<td>&nbsp;</td>
			 		<td>&nbsp;</td>
			 		<td>
			 			<input type="hidden" value="" size="15" name="id">
						<input type="submit" value="Simpan" name="submit" class="btn btn-primary">
						<input type="Reset" value="Reset" class="btn">			 		</td>
		   		</tr>
		 	</tbody>
		</table>

		</div>
		<div style="float:left;margin-left:20px">		
			<table style="width:auto">
				<tr>
					<td><label>Tgl. Persiapan</label></td>
					<td>:</td>
					<td>
						<input type="text" value="<?php echo $dataProd['tgl_persiapan'];?>" <?php echo $readonly;?> size="30" name="tgl_persiapan" id="tgl_persiapan" disabled>						
					</td>
			   	</tr>
			   	<tr>
			   	  <td><label>Status Persiapan</label></td>
			   	  <td>:</td>
			   	  <td>
			   	  	<input type="checkbox" name="status_persiapan" id="status_persiapan" disabled  <?php echo $statusPersiapan;?>>			   	  	
			   	  </td>
		   	  </tr>	
		   	  	<tr>
					<td><label>Tgl. Produksi</label></td>
					<td>:</td>
					<td>
						<input type="text" value="<?php echo $dataProd['tgl_prod'];?>" size="30" name="tgl_prod" id="tgl_prod" disabled>						
					</td>
			   	</tr>
			   	<tr>
			   	  <td><label>Status Produksi</label></td>
			   	  <td>:</td>
			   	  <td>
			   	  	<input type="checkbox" name="status_prod" id="status_prod" disabled <?php echo $statusProduksi;?>>			   	  	
			   	  </td>
		   	  </tr>	
		   	  	<tr>
					<td><label>Tgl. Selesai</label></td>
					<td>:</td>
					<td>
						<input type="text" value="<?php echo $dataProd['tgl_selesai'];?>" size="30" name="tgl_selesai" id="tgl_selesai" disabled >						
					</td>
			   	</tr>
			   	<tr>
			   	  <td><label>Status Selesai</label></td>
			   	  <td>:</td>
			   	  <td>
			   	  	<input type="checkbox" name="status_selesai" id="status_selesai" disabled <?php echo $statusSelesai;?>>			   	  	
			   	  </td>
		   	  </tr>				  
			</table>
		</form>
		</div>
	</div>


<?php
}elseif ($act=='update') {
	# code...
	$id_produksi = (int) $_POST['id_produksi'];
	$limbah_prod = $_POST['limbah_prod'];

	if(!empty($id_produksi)){

		$update = mysql_query("UPDATE tb_produksi SET limbah_prod='$limbah_prod' WHERE id_produksi='$id_produksi'");
		
		if($update)
			sukses('Data berhasil disimpan', '?pages=limbah_prod');
		else
			sukses('Data gagal disimpan', '?pages=limbah_prod');
			
	}
	
}
?>					